{extend name="admin@index_layout"/}
{block name="main"}
<div class="layui-card">
    <div class="layui-card-header">机器人列表</div>
    <div class="layui-card-body">
        <blockquote class="layui-elem-quote quoteBox">
            <form class="layui-form search-from" method="get">
                <div class="layui-inline">
                    <div class="layui-input-inline">
                        <input type="text" class="layui-input" id="username" placeholder="搜索玩家名称">
                    </div>
                    <a class="layui-btn search_btn" data-type="reload" id="search_name">搜索</a>
                </div>
                <div class="layui-inline">
                    <select name="usergamingstate" lay-verify="required" lay-filter="usergamingstate">
                        <option value="0" selected>正常</option>
                        <option value="1">准备</option>
                        <option value="2">游戏中</option>
                        <option value="3">掉线</option>
                        <option value="4">排队</option>
                    </select>
                </div>
                <div class = "layui-inline">
                    <div class="layui-form-item">
                        <label class="layui-form-label">所属游戏</label>
                        <div class="layui-input-inline">
                            <select name="gameid" id="gameid" lay-verify="required" lay-filter="gameid">
                                {volist name="games" id="vo"}
                                <option value="{$vo['id']}">{$vo['name']}</option>
                                {/volist}
                            </select>
                        </div>
                        <label class="layui-form-label">所属房间</label>
                        <div class="layui-input-inline">
                            <select name="roomid" id="roomid" lay-verify="required">
                                {volist name="servers" id="servervo"}
                                <option value="{$servervo['id']}">{$servervo['servername']}</option>
                                {/volist}
                            </select>
                        </div>
                        <a class="layui-btn search_btn" data-type="reload" id="search_room">搜索</a>
                    </div>
                </div>
            </form>
        </blockquote>
        <table class="layui-hide" id="dataTable" lay-filter="dataTable"></table>
    </div>
</div>
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <a class="layui-btn layui-btn-sm layui-btn-danger confirm layui-batch-all" data-href='{:url("delete")}' isMember="2">批量删除</a>
        <a class="layui-btn layui-btn-sm" href='{:url("add")}'>添加机器人</a>
        <a class="layui-btn layui-btn-sm" href='{:url("updateandroidmoney")}'>加钱</a>
    </div>
</script>
<script type="text/html" id="barTool">
    <a href='{:url("edit")}?id={{ d.userid }}' class="layui-btn layui-btn-xs">注册数据</a>
    <a href='{:url("gamedata")}?id={{ d.userid }}' class="layui-btn layui-btn-xs">游戏数据</a>
    <a href='{:url("delete")}?ids={{ d.userid }}' class="layui-btn layui-btn-danger layui-btn-xs layui-tr-del">删除</a>
</script>
{/block}
{block name="script"}
<script>
layui.use('table', function() {
    var table = layui.table,
        $ = layui.$,
        form = layui.form;
    table.render({
        elem: '#dataTable',
        toolbar: '#toolbarDemo',
        url: '{:url("manage")}',
        cols: [
            [
                { type: 'checkbox', fixed: 'left' },
                { field: 'userid', width: 80, title: '用户ID' },
                { field: 'username', width: 250, title: '用户名' },
                { field: 'nullity', width: 100, title: '状态' },
                { field: 'name',  title: '所属游戏' },
                { field: 'servername',  title: '所属房间' },
                { field: 'money',  title: '金币' },
                { field: 'bankmoney',  title: '保险箱金币' },
                { fixed: 'right', width: 220, title: '操作', templet: '#barTool' }
            ]
        ],
        page: {}
    });

    $("#search_name").on("click", function() {
        table.reload("dataTable", {
            page: {
                curr: 1 //重新从第 1 页开始
            },
            where: {
                username: $("#username").val()
            }
        })
    });


    form.on('select(usergamingstate)', function(data) {
        table.reload("dataTable", {
            page: {
                curr: 1 //重新从第 1 页开始
            },
            where: {
                curgamingstate: data.value
            }
        })
    });

    form.on('select(gameid)', function(data) {
        $.post(
            '{:url("robotmanage/getgamerooms")}',
            {
                game_id:data.value
            },
            function(d){
                var server = document.getElementById("roomid");
                var selectOptions = server.options;
                var optionLength = selectOptions.length;
                for(var i=0;i <optionLength;i++)
                {
                    server.removeChild(selectOptions[0]);
                }

                var xqo = eval('(' + d + ')');
                for(var i in xqo){
                    var option = document.createElement("option");  // 创建添加option属性
                    option.setAttribute("value",xqo[i].id); // 给option的value添加值
                    option.innerText=xqo[i].servername;     // 打印option对应的纯文本
                    server.appendChild(option);           //给select添加option子标签
                }

                form.render("select");            // 刷性select，显示出数据
            }
        );
    });

    $("#search_room").on("click", function() {
        table.reload("dataTable", {
            page: {
                curr: 1 //重新从第 1 页开始
            },
            where: {
                gameid: $("#gameid").val(),
                roomid: $("#roomid").val()
            }
        })
    });
});
</script>
{/block}